<?php
/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2017/12/19
 * Time: 下午4:10
 */

defined('BASEPATH') or exit('No direct script access allowed');
require APPPATH . '/libraries/pay/WxPay.Api.php';
require APPPATH . '/libraries/pay/PayNotifyCallBack.php';

class Wxrefund extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('form_validation'); //加载表单验证类库
        $this->load->helper('json');
        $this->load->helper('common');
        $this->output->set_content_type('application/html;charset=utf-8');
    }

    /**
     * @SWG\Post(
     *     summary="微信自动退款",
     *     path="/wxrefund",
     *     tags={"客户端"},
     *     operationId="30",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Parameter(
     *         description="退款编号",
     *         in="formData",
     *         name="refund_bn",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         description="订单transaction_id",
     *         in="formData",
     *         name="transaction_id",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         description="订单总金额",
     *         in="formData",
     *         name="total_fee",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         description="退款金额",
     *         in="formData",
     *         name="refund_fee",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    public function index($param)
    {
        if($param){
          $post = $param;
        }else{
          $post = $this->input->post();
        }
        if ($post['refund_bn'] && $post['transaction_id'] && $post['total_fee'] && $post['refund_fee']) {
            //初始化值对象
            $input = new WxPayRefund();

            $input->SetTransaction_id($post['transaction_id']);

            $input->SetTotal_fee($post['total_fee']*100);

            $input->SetOut_refund_no($post['refund_bn']);

            $input->SetRefund_fee($post['refund_fee']*100);

            $input->SetOp_user_id('1251347001');

            $order = WxPayApi::refund($input);

            send_json(array('code' => 2000, 'data' => $order, 'msg' => "退款成功"));

            //header("Content-Type: application/json");
        } else {
            send_json(array('code' => 4000, 'data' => '', 'msg' => "缺少参数"));
        }
    }
}
